﻿namespace HRM.CORE.Process
{
    using DevExpress.Utils;
    using DevExpress.XtraEditors;
    using DevExpress.XtraEditors.Controls;
    using DevExpress.XtraGrid.Columns;
    using DevExpress.XtraGrid.Views.Grid;
    using DevExpress.XtraLayout;
    using DevExpress.XtraLayout.Utils;
    using APP.HELPER;
    using HRM.CORE;
    
    using System; using Extra.ERP;
    using System.ComponentModel;
    using System.Drawing;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms; using Common;
    using Common;
    using APP.HELPER.Utils.Language;
    using Security.ERP;

    public class xucAdvanceAdd : Common.xucBaseAddH
    {
        private CalcEdit calMoney;
        private TextEdit cboPerson;
        private GridColumn colEmployeeCode;
        private GridColumn colFirstName;
        private GridColumn colLastName;
        private IContainer components = null;
        private DateEdit dtDate;
        private GridLookUpEdit glkEmployeeCode;
        private GridView gridLookUpEdit1View;
        private GroupControl grInformation;
        private LabelControl labelControl1;
        private LabelControl labelControl2;
        private LayoutControl layoutControl1;
        private LayoutControlGroup layoutControlGroup1;
        private LayoutControlItem lcDate;
        private LayoutControlItem lcMoney;
        private LayoutControlItem lcPerson;
        private LayoutControlItem lcReason;
        private Guid m_AdvanceID = Guid.Empty;
        private string m_EmployeeCode = "";
        private TextEdit txtEmployeeName;
        private TextEdit txtReason;

        public event SuccessEventHander Success;

        public xucAdvanceAdd()
        {
            this.InitializeComponent();
            this.InitData();
            this.InitComponent();
        }

        protected override void Add()
        {
            base.Add();
            HRM_PROCESS_ADVANCE hrm_process_advance = new HRM_PROCESS_ADVANCE();
        }

        public void Clear()
        {
            this.txtReason.Text = "";
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void glkEmployeeCode_EditValueChanged(object sender, EventArgs e)
        {
            this.m_EmployeeCode = this.glkEmployeeCode.EditValue.ToString();
            this.LoadEmployeeInformation();
        }

        protected override void Init()
        {
        }

        private void InitComponent()
        {
            this.calMoney.Text = "0";
            this.dtDate.DateTime = DateTime.Now;
        }

        private void InitData()
        {
            new HRM_EMPLOYEE().AddGridLookupEdit(this.glkEmployeeCode);
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(xucAdvanceAdd));
            this.grInformation = new GroupControl();
            this.labelControl2 = new LabelControl();
            this.labelControl1 = new LabelControl();
            this.txtEmployeeName = new TextEdit();
            this.glkEmployeeCode = new GridLookUpEdit();
            this.gridLookUpEdit1View = new GridView();
            this.colEmployeeCode = new GridColumn();
            this.colFirstName = new GridColumn();
            this.colLastName = new GridColumn();
            this.layoutControl1 = new LayoutControl();
            this.txtReason = new TextEdit();
            this.dtDate = new DateEdit();
            this.cboPerson = new TextEdit();
            this.calMoney = new CalcEdit();
            this.layoutControlGroup1 = new LayoutControlGroup();
            this.lcReason = new LayoutControlItem();
            this.lcDate = new LayoutControlItem();
            this.lcMoney = new LayoutControlItem();
            this.lcPerson = new LayoutControlItem();
            ((ISupportInitialize) base.Err).BeginInit();
            base.imageCollection2.BeginInit();
            this.grInformation.BeginInit();
            this.grInformation.SuspendLayout();
            this.txtEmployeeName.Properties.BeginInit();
            this.glkEmployeeCode.Properties.BeginInit();
            this.gridLookUpEdit1View.BeginInit();
            this.layoutControl1.BeginInit();
            this.layoutControl1.SuspendLayout();
            this.txtReason.Properties.BeginInit();
            this.dtDate.Properties.VistaTimeProperties.BeginInit();
            this.dtDate.Properties.BeginInit();
            this.cboPerson.Properties.BeginInit();
            this.calMoney.Properties.BeginInit();
            this.layoutControlGroup1.BeginInit();
            this.lcReason.BeginInit();
            this.lcDate.BeginInit();
            this.lcMoney.BeginInit();
            this.lcPerson.BeginInit();
            base.SuspendLayout();
            base.btnSave.Location = new Point(0x18, 0xe5);
            base.btnSave.TabIndex = 1;
            base.btnCancel.Location = new Point(0xe0, 0xe5);
            base.btnCancel.TabIndex = 3;
            base.btnSaveNew.Location = new Point(0x7b, 0xe5);
            base.btnSaveNew.TabIndex = 2;
            base.imageCollection2.ImageStream = (ImageCollectionStreamer) manager.GetObject("imageCollection2.ImageStream");
            this.grInformation.Controls.Add(this.labelControl2);
            this.grInformation.Controls.Add(this.labelControl1);
            this.grInformation.Controls.Add(this.txtEmployeeName);
            this.grInformation.Controls.Add(this.glkEmployeeCode);
            this.grInformation.Controls.Add(this.layoutControl1);
            this.grInformation.Dock = DockStyle.Top;
            this.grInformation.Location = new Point(0, 0);
            this.grInformation.Name = "grInformation";
            this.grInformation.Size = new Size(0x148, 220);
            this.grInformation.TabIndex = 0;
            this.grInformation.Text = "Th\x00f4ng Tin Tạm Ứng";
            this.labelControl2.AllowHtmlString = true;
            this.labelControl2.Location = new Point(8, 0x37);
            this.labelControl2.Name = "labelControl2";
            this.labelControl2.Size = new Size(0x56, 14);
            this.labelControl2.TabIndex = 2;
            this.labelControl2.Text = "Họ t\x00ean nh\x00e2n vi\x00ean:";
            this.labelControl1.AllowHtmlString = true;
            this.labelControl1.Location = new Point(8, 0x1d);
            this.labelControl1.Name = "labelControl1";
            this.labelControl1.Size = new Size(0x60, 14);
            this.labelControl1.TabIndex = 0;
            this.labelControl1.Text = "Chọn nh\x00e2n vi\x00ean <color=red>(*)</color>:";
            this.txtEmployeeName.Enabled = false;
            this.txtEmployeeName.Location = new Point(0x6b, 0x35);
            this.txtEmployeeName.Name = "txtEmployeeName";
            this.txtEmployeeName.Size = new Size(0xd5, 20);
            this.txtEmployeeName.TabIndex = 3;
            this.glkEmployeeCode.Location = new Point(0x6b, 0x1b);
            this.glkEmployeeCode.Name = "glkEmployeeCode";
            this.glkEmployeeCode.Properties.Buttons.AddRange(new EditorButton[] { new EditorButton(ButtonPredefines.Combo) });
            this.glkEmployeeCode.Properties.NullText = "[Chọn nh\x00e2n vi\x00ean]";
            this.glkEmployeeCode.Properties.View = this.gridLookUpEdit1View;
            this.glkEmployeeCode.Size = new Size(0xd5, 20);
            this.glkEmployeeCode.TabIndex = 1;
            this.glkEmployeeCode.EditValueChanged += new EventHandler(this.glkEmployeeCode_EditValueChanged);
            this.gridLookUpEdit1View.Columns.AddRange(new GridColumn[] { this.colEmployeeCode, this.colFirstName, this.colLastName });
            this.gridLookUpEdit1View.FocusRectStyle = DrawFocusRectStyle.RowFocus;
            this.gridLookUpEdit1View.Name = "gridLookUpEdit1View";
            this.gridLookUpEdit1View.OptionsSelection.EnableAppearanceFocusedCell = false;
            this.gridLookUpEdit1View.OptionsView.ShowAutoFilterRow = true;
            this.gridLookUpEdit1View.OptionsView.ShowGroupPanel = false;
            this.colEmployeeCode.Caption = "M\x00e3 nh\x00e2n vi\x00ean";
            this.colEmployeeCode.FieldName = "EmployeeCode";
            this.colEmployeeCode.Name = "colEmployeeCode";
            this.colEmployeeCode.Visible = true;
            this.colEmployeeCode.VisibleIndex = 0;
            this.colFirstName.Caption = "Họ l\x00f3t";
            this.colFirstName.FieldName = "FirstName";
            this.colFirstName.Name = "colFirstName";
            this.colFirstName.Visible = true;
            this.colFirstName.VisibleIndex = 1;
            this.colLastName.Caption = "T\x00ean";
            this.colLastName.FieldName = "LastName";
            this.colLastName.Name = "colLastName";
            this.colLastName.Visible = true;
            this.colLastName.VisibleIndex = 2;
            this.layoutControl1.Controls.Add(this.txtReason);
            this.layoutControl1.Controls.Add(this.dtDate);
            this.layoutControl1.Controls.Add(this.cboPerson);
            this.layoutControl1.Controls.Add(this.calMoney);
            this.layoutControl1.Location = new Point(4, 0x4b);
            this.layoutControl1.Name = "layoutControl1";
            this.layoutControl1.OptionsView.AllowHotTrack = true;
            this.layoutControl1.OptionsView.HighlightDisabledItem = true;
            this.layoutControl1.OptionsView.HighlightFocusedItem = true;
            this.layoutControl1.Root = this.layoutControlGroup1;
            this.layoutControl1.Size = new Size(320, 0x8b);
            this.layoutControl1.TabIndex = 4;
            this.layoutControl1.Text = "layoutControl1";
            this.txtReason.Location = new Point(0x67, 6);
            this.txtReason.Name = "txtReason";
            this.txtReason.Properties.Appearance.Options.UseTextOptions = true;
            this.txtReason.Properties.Appearance.TextOptions.VAlignment = VertAlignment.Top;
            this.txtReason.Properties.Appearance.TextOptions.WordWrap = WordWrap.Wrap;
            this.txtReason.Properties.AutoHeight = false;
            this.txtReason.Size = new Size(0xd5, 0x2b);
            this.txtReason.StyleController = this.layoutControl1;
            this.txtReason.TabIndex = 0;
            this.dtDate.EditValue = null;
            this.dtDate.Location = new Point(0x67, 0x39);
            this.dtDate.Name = "dtDate";
            this.dtDate.Properties.Buttons.AddRange(new EditorButton[] { new EditorButton(ButtonPredefines.Combo) });
            this.dtDate.Properties.VistaTimeProperties.Buttons.AddRange(new EditorButton[] { new EditorButton() });
            this.dtDate.Size = new Size(0xd5, 20);
            this.dtDate.StyleController = this.layoutControl1;
            this.dtDate.TabIndex = 1;
            this.cboPerson.Location = new Point(0x67, 0x71);
            this.cboPerson.Name = "cboPerson";
            this.cboPerson.Size = new Size(0xd5, 20);
            this.cboPerson.StyleController = this.layoutControl1;
            this.cboPerson.TabIndex = 3;
            this.calMoney.Location = new Point(0x67, 0x55);
            this.calMoney.Name = "calMoney";
            this.calMoney.Properties.Buttons.AddRange(new EditorButton[] { new EditorButton(ButtonPredefines.Combo) });
            this.calMoney.Size = new Size(0xd5, 20);
            this.calMoney.StyleController = this.layoutControl1;
            this.calMoney.TabIndex = 2;
            this.layoutControlGroup1.CustomizationFormText = "layoutControlGroup1";
            this.layoutControlGroup1.EnableIndentsWithoutBorders = DefaultBoolean.True;
            this.layoutControlGroup1.GroupBordersVisible = false;
            this.layoutControlGroup1.Items.AddRange(new BaseLayoutItem[] { this.lcReason, this.lcDate, this.lcMoney, this.lcPerson });
            this.layoutControlGroup1.Location = new Point(0, 0);
            this.layoutControlGroup1.Name = "layoutControlGroup1";
            this.layoutControlGroup1.Padding = new DevExpress.XtraLayout.Utils.Padding(2, 2, 2, 2);
            this.layoutControlGroup1.Size = new Size(320, 0x8b);
            this.layoutControlGroup1.Spacing = new DevExpress.XtraLayout.Utils.Padding(0, 0, 0, 0);
            this.layoutControlGroup1.Text = "layoutControlGroup1";
            this.layoutControlGroup1.TextVisible = false;
            this.lcReason.Control = this.txtReason;
            this.lcReason.CustomizationFormText = "L\x00fd do tạm ứng:";
            this.lcReason.Location = new Point(0, 0);
            this.lcReason.Name = "lcReason";
            this.lcReason.Size = new Size(0x13c, 0x33);
            this.lcReason.Spacing = new DevExpress.XtraLayout.Utils.Padding(0, 0, 2, 2);
            this.lcReason.Text = "L\x00fd do tạm ứng:";
            this.lcReason.TextSize = new Size(0x5f, 13);
            this.lcDate.Control = this.dtDate;
            this.lcDate.CustomizationFormText = "Ng\x00e0y tạm ứng:";
            this.lcDate.Location = new Point(0, 0x33);
            this.lcDate.Name = "lcDate";
            this.lcDate.Size = new Size(0x13c, 0x1c);
            this.lcDate.Spacing = new DevExpress.XtraLayout.Utils.Padding(0, 0, 2, 2);
            this.lcDate.Text = "Ng\x00e0y tạm ứng:";
            this.lcDate.TextSize = new Size(0x5f, 13);
            this.lcMoney.Control = this.calMoney;
            this.lcMoney.CustomizationFormText = "Số tiền tạm ứng:";
            this.lcMoney.Location = new Point(0, 0x4f);
            this.lcMoney.Name = "lcMoney";
            this.lcMoney.Size = new Size(0x13c, 0x1c);
            this.lcMoney.Spacing = new DevExpress.XtraLayout.Utils.Padding(0, 0, 2, 2);
            this.lcMoney.Text = "Số tiền tạm ứng:";
            this.lcMoney.TextSize = new Size(0x5f, 13);
            this.lcPerson.Control = this.cboPerson;
            this.lcPerson.CustomizationFormText = "layoutControlItem1";
            this.lcPerson.Location = new Point(0, 0x6b);
            this.lcPerson.Name = "lcPerson";
            this.lcPerson.Size = new Size(0x13c, 0x1c);
            this.lcPerson.Spacing = new DevExpress.XtraLayout.Utils.Padding(0, 0, 2, 2);
            this.lcPerson.Text = "Người cho tạm ứng:";
            this.lcPerson.TextSize = new Size(0x5f, 13);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.grInformation);
            base.Name = "xucAdvanceAdd";
            base.Size = new Size(0x148, 0x108);
            base.Controls.SetChildIndex(base.btnCancel, 0);
            base.Controls.SetChildIndex(base.btnSave, 0);
            base.Controls.SetChildIndex(base.btnSaveNew, 0);
            base.Controls.SetChildIndex(this.grInformation, 0);
            ((ISupportInitialize) base.Err).EndInit();
            base.imageCollection2.EndInit();
            this.grInformation.EndInit();
            this.grInformation.ResumeLayout(false);
            this.grInformation.PerformLayout();
            this.txtEmployeeName.Properties.EndInit();
            this.glkEmployeeCode.Properties.EndInit();
            this.gridLookUpEdit1View.EndInit();
            this.layoutControl1.EndInit();
            this.layoutControl1.ResumeLayout(false);
            this.txtReason.Properties.EndInit();
            this.dtDate.Properties.VistaTimeProperties.EndInit();
            this.dtDate.Properties.EndInit();
            this.cboPerson.Properties.EndInit();
            this.calMoney.Properties.EndInit();
            this.layoutControlGroup1.EndInit();
            this.lcReason.EndInit();
            this.lcDate.EndInit();
            this.lcMoney.EndInit();
            this.lcPerson.EndInit();
            base.ResumeLayout(false);
        }

        private void InitMultiLanguages()
        {
            this.grInformation.Text = MultiLanguages.GetString("tbl_ProcessAdvanceAdd", "grInformation", this.grInformation.Text);
            this.lcReason.Text = MultiLanguages.GetString("tbl_ProcessAdvanceAdd", "Reason", this.lcReason.Text);
            this.lcDate.Text = MultiLanguages.GetString("tbl_ProcessAdvanceAdd", "Date", this.lcDate.Text);
            this.lcMoney.Text = MultiLanguages.GetString("tbl_ProcessAdvanceAdd", "Money", this.lcMoney.Text);
            this.lcPerson.Text = MultiLanguages.GetString("tbl_ProcessAdvanceAdd", "Person", this.lcPerson.Text);
        }

        private void LoadEmployeeInformation()
        {
            HRM_EMPLOYEE hrm_employee = new HRM_EMPLOYEE();
            hrm_employee.Get(this.m_EmployeeCode);
            this.grInformation.Text = "Th\x00f4ng Tin Tạm Ứng - " + hrm_employee.EmployeeCode + " (" + hrm_employee.FirstName + " " + hrm_employee.LastName + ")";
            this.txtEmployeeName.Text = hrm_employee.FirstName + " " + hrm_employee.LastName;
        }

        private void RaiseSuccessEventHander(HRM_PROCESS_ADVANCE item)
        {
            if (this.Success != null)
            {
                this.Success(this, item);
            }
        }

        public void SetCode(string EmployeeCode)
        {
            this.m_EmployeeCode = EmployeeCode;
            if (this.m_EmployeeCode != "")
            {
                this.glkEmployeeCode.EditValue = this.m_EmployeeCode;
                this.LoadEmployeeInformation();
            }
        }

        public void SetData(HRM_PROCESS_ADVANCE item)
        {
            this.m_AdvanceID = item.AdvanceID;
            SYS_LOG.Insert("Qu\x00e1 Tr\x00ecnh Tạm Ứng", "Xem", this.m_AdvanceID.ToString());
            this.m_EmployeeCode = item.EmployeeCode;
            this.glkEmployeeCode.EditValue = this.m_EmployeeCode;
            this.glkEmployeeCode.Enabled = false;
            this.txtReason.Text = item.Reason;
            this.dtDate.DateTime = item.Date;
            this.calMoney.Text = item.Money.ToString();
            this.cboPerson.Text = item.Person;
            this.LoadEmployeeInformation();
        }

        protected override string uc_Change()
        {
            return string.Empty;
        }

        protected override string uc_Delete()
        {
            HRM_PROCESS_ADVANCE item = new HRM_PROCESS_ADVANCE {
                AdvanceID = this.m_AdvanceID
            };
            string str = item.Delete();
            if (str == "OK")
            {
                this.RaiseSuccessEventHander(item);
            }
            return str;
        }

        protected override string uc_Save()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiProcessAdvance") != "OK")
            {
                return "";
            }
            if (!MyRule.AllowAdd)
            {
                MyRule.Notify();
                return "";
            }
            if (this.m_EmployeeCode == "")
            {
                XtraMessageBox.Show("Vui l\x00f2ng chọn nh\x00e2n vi\x00ean để lưu dữ liệu!", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK);
                return "";
            }
            SYS_LOG.Insert("Qu\x00e1 Tr\x00ecnh Tạm Ứng", "Th\x00eam", this.m_AdvanceID.ToString());
            base.SetWaitDialogCaption("Đang lưu dữ liệu...");
            Cursor.Current = Cursors.WaitCursor;
            HRM_PROCESS_ADVANCE item = new HRM_PROCESS_ADVANCE {
                AdvanceID = Guid.NewGuid(),
                EmployeeCode = this.m_EmployeeCode,
                Reason = this.txtReason.Text,
                Date = this.dtDate.DateTime,
                Money = decimal.Parse(this.calMoney.Text.ToString()),
                Person = this.cboPerson.Text
            };
            string text = item.Insert();
            if (text == "OK")
            {
                this.RaiseSuccessEventHander(item);
            }
            Cursor.Current = Cursors.Default;
            this.DoHide();
            if (text != "OK")
            {
                XtraMessageBox.Show(text, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            return text;
        }

        protected override string uc_Update()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiProcessAdvance") != "OK")
            {
                return "";
            }
            if (!MyRule.AllowEdit)
            {
                MyRule.Notify();
                return "";
            }
            SYS_LOG.Insert("Qu\x00e1 Tr\x00ecnh Tạm Ứng", "Cập Nhật", this.m_AdvanceID.ToString());
            base.SetWaitDialogCaption("Đang cập nhật dữ liệu...");
            HRM_PROCESS_ADVANCE item = new HRM_PROCESS_ADVANCE {
                AdvanceID = this.m_AdvanceID,
                EmployeeCode = this.m_EmployeeCode,
                Reason = this.txtReason.Text,
                Date = this.dtDate.DateTime,
                Money = decimal.Parse(this.calMoney.Text.ToString()),
                Person = this.cboPerson.Text
            };
            string text = item.Update();
            if (text == "OK")
            {
                this.RaiseSuccessEventHander(item);
            }
            if (text != "OK")
            {
                XtraMessageBox.Show(text, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            this.DoHide();
            return text;
        }

        public delegate void SuccessEventHander(object sender, HRM_PROCESS_ADVANCE item);
    }
}

